home *** CD-ROM | disk | FTP | other *** search
/ Universität Tübingen - 1997/1998 Winter / Universität Tübingen - Wintersemester 1997-98 - Uni-Informationssystem und Stadt-Informationssystem.iso / ajs / rainbowt.jav < prev    next >
Text File  |  1997-08-14  |  5KB  |  191 lines

  1. /** RainbowText.java
  2.     Display text in changing rainbow colors.
  3.     @author Glenn A. Richard
  4.     @version 2.0, November 24, 1996
  5.     Center for High Pressure Research
  6.     ESS Building
  7.     SUNY at Stony Brook
  8.     Stony Brook, NY 11794
  9. */
  10. import java.awt.*;
  11.  
  12. public class RainbowText extends java.applet.Applet
  13.     implements Runnable {
  14.   private String textStr = null;
  15.   private String fontName;
  16.   private int fontStyle;
  17.   private int fontSize;
  18.   private Color bgColor;
  19.   private int sleepTime;
  20.   private String paramStr;
  21.   private int strlen;
  22.   private Thread runner = null;
  23.   private char theChars[];
  24.   private int charOffsets[];
  25.   private Color colors[];
  26.   private int yOffset;
  27.   private int phase = 0;
  28.   private Image offScreenImage;
  29.   private Graphics offScreenG;
  30.   private Font f;
  31.   private FontMetrics fm;
  32.   private boolean stopped = false;
  33.   private String pinfo[][]={
  34.     {"text", "String", "Text to display (RainbowText)"},
  35.     {"bgcolor", "hex rrggbb color", "Background color in rrggbb format (000000)"},
  36.     {"fontname", "String", "Name of the font (TimesRoman)"},
  37.     {"fontstyle", "String", "B = bold, I = italic, BI = both ()"},
  38.     {"fontsize", "int", "Point size to be used (36)"},
  39.     {"sleeptime", "int", "Milliseconds for sleep time (100)"}
  40.   };
  41.  
  42.   public void init() {
  43.     float h;
  44.     int xPos=20;
  45.     paramStr = getParameter("bgcolor");
  46.     if (paramStr == null)
  47.       bgColor = Color.black;
  48.     else try {
  49.       bgColor = new Color(Integer.parseInt(paramStr, 16));
  50.     }
  51.     catch (NumberFormatException e) {
  52.       bgColor=Color.black;
  53.     }
  54.     setBackground(bgColor);
  55.     textStr = getParameter("text");
  56.     if (textStr == null) {
  57.       textStr = "RainbowText";
  58.     }
  59.     fontName = getParameter("fontname");
  60.     if (fontName == null) {
  61.       fontName = "TimesRoman";
  62.     }
  63.     paramStr = getParameter("fontstyle");
  64.     if (paramStr == null)
  65.       fontStyle = Font.PLAIN;
  66.     else if (paramStr.equals("B"))
  67.       fontStyle = Font.BOLD;
  68.     else if (paramStr.equals("I"))
  69.       fontStyle = Font.ITALIC;
  70.     else if (paramStr.equals("BI"))
  71.       fontStyle = Font.BOLD | Font.ITALIC;
  72.     else
  73.       fontStyle = Font.PLAIN;
  74.     paramStr = getParameter("fontsize");
  75.     if (paramStr == null)
  76.       fontSize = 36;
  77.     else try {
  78.       fontSize = Integer.parseInt(paramStr);
  79.     } catch (Exception e) {
  80.       fontSize = 36;
  81.     }
  82.     paramStr = getParameter("sleeptime");
  83.     if (paramStr == null)
  84.       sleepTime = 100;
  85.     else try {
  86.       sleepTime = Integer.parseInt(paramStr);
  87.     } catch (Exception e) {
  88.       sleepTime = 100;
  89.     }
  90.     f=new Font(fontName,fontStyle,fontSize);
  91.     fm=getFontMetrics(f);
  92.     resize(40+fm.stringWidth(textStr),10+fm.getHeight());
  93.     yOffset = fm.getAscent()+5;
  94.     strlen = textStr.length();
  95.     theChars =  new char [strlen];
  96.     charOffsets = new int [strlen];
  97.     textStr.getChars(0,strlen,theChars,0);
  98.     colors = new Color[strlen];
  99.     for (int i = 0; i < strlen; i++) {
  100.       h = ((float)i)/((float)strlen);
  101.       colors[i] = new Color(Color.HSBtoRGB(h,1.0f,1.0f));
  102.       charOffsets[i] = xPos;
  103.       xPos+=fm.charWidth(theChars[i]);
  104.     }
  105.     offScreenImage = createImage(this.size().width,this.size().height);
  106.     offScreenG = offScreenImage.getGraphics();
  107.     offScreenG.setColor(bgColor);
  108.     offScreenG.fillRect(0,0,this.size().width,this.size().height);
  109.     offScreenG.setFont(f);
  110.   }
  111.  
  112.   public void start() {
  113.     if(runner == null) {
  114.       runner = new Thread(this);
  115.       runner.start();
  116.     }
  117.   }
  118.  
  119.   public void stop() {
  120.     if (runner != null && runner.isAlive())
  121.       runner.stop();
  122.     runner = null;
  123.   }
  124.  
  125.   public void run() {
  126.     showStatus("Click to stop "+getClass().getName());
  127.     while (runner != null) {
  128.       repaint();
  129.       try {
  130.         Thread.sleep(sleepTime);
  131.       }
  132.       catch (InterruptedException e) { }
  133.     }
  134.   }
  135.  
  136.   public void update(Graphics g) {
  137.     int x, y;
  138.     phase--;
  139.     if (phase < 0)
  140.       phase=strlen-1;
  141.     for(int i=0;i<strlen;i++)
  142.      {
  143.        x = charOffsets[i];
  144.        offScreenG.setColor(colors[(phase+i)%strlen]);
  145.        offScreenG.drawChars(theChars,i,1,x,yOffset);
  146.      }
  147.     paint(g);
  148.   }
  149.  
  150.   public void paint(Graphics g) {
  151.      g.drawImage(offScreenImage,0,0,this);
  152.   }
  153.  
  154.   public boolean handleEvent(Event e) {
  155.     if (e.id == Event.MOUSE_DOWN) {
  156.         if (runner != null && runner.isAlive()) {
  157.             if (stopped) {
  158.                 showStatus("Click to stop "+getClass().getName());
  159.                 runner.resume();
  160.             }
  161.             else {
  162.                 showStatus("Click to restart "+getClass().getName());
  163.                 runner.suspend();
  164.             }
  165.             stopped = !stopped;
  166.         }
  167.         else {
  168.             stopped = false;
  169.             runner = new Thread(this);
  170.             runner.start();
  171.         }
  172.     }
  173.     else if (e.id == Event.MOUSE_ENTER)
  174.       if (stopped)
  175.         showStatus("Click to restart "+getClass().getName());
  176.       else
  177.         showStatus("Click to stop "+getClass().getName());
  178.     else return super.handleEvent(e);
  179.     return true;
  180.   }
  181.  
  182.   public String[][] getParameterInfo() {
  183.     return pinfo;
  184.   }
  185.  
  186.   public String getAppletInfo() {
  187.     return("Glenn A. Richard, CHiPR, SUNY at Stony Brook");
  188.   }
  189. }
  190.  
  191.